<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <title>商品后台管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <link rel="stylesheet" href="./css/all.css">
    <link rel="stylesheet" href="./css/bootstrap.css">
    <link rel="stylesheet" href="./css/course_category.css">
</head>

<body>
    <div id="app">

        <nav class="navbar">
            <div class="container" style="height: 80px;background: #58a3f9">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <a class="navbar-brand" href="#">
                        <img src="image/easy-pay.png">
                    </a>
                </div>
                <ul class="nav navbar-nav navbar-right" style="margin-top: 30px">
                    <li><a href="#"><span class="inner">首页</span></a></li>
                    <li>
                        <a href="#">
                            <span class="inner">商品管理</span></a>
                    </li>
                    <li>
                        <a href="#">
                            <span class="inner">购物车管理</span>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <span class="inner">用户管理</span></a>
                    </li>
                </ul>
            </div>
        </nav>
        <div style="min-height: 400px">
            <div class="container">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4>商品管理-修改商品</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">

                            <div class="form-group">
                                <label for="id" class="col-sm-3 control-label">商品编号</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="id" v-model="good.id">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="title" class="col-sm-3 control-label">商品标题</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="title" v-model="good.title">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="content" class="col-sm-3 control-label">商品描述</label>
                                <div class="col-sm-8">
                                    <textarea class="form-control" rows="3" style="resize: none"
                                        id="content" v-model="good.content"></textarea>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="imagePath" class="col-sm-3 control-label">图片路径</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="imagePath" v-model="good.imagePath">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="price" class="col-sm-3 control-label">商品价格</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="price" v-model="good.price">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-8 col-sm-offset-3">
                                    <p class="form-control-static text-danger">{{msg}}</p>
                                </div>
                            </div>
                            <div style="text-align: right">
                                <button type="button" class="btn btn-primary" id="assure" @click="updateGoods">确定</button>
                                <button type="button" class="btn btn-default">取消</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <!-- 请在此处添加页脚 -->
        <footer>
            <div  style="background: #58a3f9;color: #fff;" class="container">
                <p class="text-center">易购集团</p>
                <p class="text-center">Copyright 2019</p>
            </div>
        </footer>
    </div>

</body>

</html>
<script src="./js/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    //这里ajax实现和后台交互
    new Vue({
        el: "#app",
        data: {
            data: undefined,
            good: {
                id: "",
                title: "",
                content: "",
                imagePath: "",
                price: ""
            },
            msg:"在此显示错误信息"
        },
        methods: {
            updateGoods(){
                var that = this;
                var priscType = !/^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})?$/;
                if(that.good.title==""&&that.good.imagePath==""&&that.good.price==""){
                    // if(/^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})?$/.test(that.good.price)){
                    //     that.msg="商品的價格格式出錯，必須是正數，最多兩位小數點。"
                    // return false;
                    // }
                    that.msg="商品格式出錯,請檢查后重試"
                    return false;
                }
                axios.post('http://127.0.0.1:8080/h5/goods/updateGoods',that.good)
                .then(function (res) {
                    console.log(res);
                    if (res.status == 200 && res.data.success == true) {
                        that.msg = res.data.msg
                    }

                })
                .catch(function (err) {
                    console.log(err);
                });
            }
        },
        created() {
            var that = this;
            axios.post('http://127.0.0.1:8080/h2/goods/getGoods', 
            {
                id:1
            })
                .then(function (res) {
                    console.log(res);
                    if (res.status == 200 && res.data.success == true) {
                        console.log("asd");
                        that.good = res.data.data
                        console.log(that.good);
                    }

                })
                .catch(function (err) {
                    console.log(err);
                });
        }
    })
</script>